package com.note.config.service;

import com.alibaba.fastjson.JSON;
import com.note.common.constants.RabbitConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Package：com.note.user.service
 * @Name：MqServiceImpl
 * @Author：热伊木
 * @Email：uyevan@163.com
 * @Date：2024-05-01-19:21
 * @Description：
 */
@Slf4j
@Service
//@RequiredArgsConstructor/*依赖注入*/
public class MqServiceImpl {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Async //异步执行
    public void sendMessage(Object object) {
        long startTime = System.currentTimeMillis();
        //TODO 非阻塞发送
        rabbitTemplate.convertAndSend(
                RabbitConst.Exchange,
                RabbitConst.RotingKeyLog, JSON.toJSONString(object));
        //TODO 阻塞发送
        /*rabbitTemplate.convertSendAndReceive(
                RabbitConst.Exchange,
                RabbitConst.RotingKeyLog,JSON.toJSONString(object));*/
        long endTime = System.currentTimeMillis();
        //log.error("time:%dms".formatted(endTime - startTime));
    }
}
